package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.po.Form;
import com.jeff.po.FormTh;
import com.jeff.service.FormService;
import com.jeff.service.FormThService;
import com.jeff.util.ResultMap;
import com.jeff.vo.FormInsertVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("form")
public class FormController {

    @Autowired
    public FormService formService;
    @Autowired
    public FormThService formThService;

    @RequestMapping("insertForm")
    @ResponseBody
    public ResultMap add(HttpServletRequest request, @RequestBody FormInsertVo formInsertVo) {
        ResultMap rm = new ResultMap();
        try {
            Form form = formInsertVo.getForm();
            form.setState(mConst.STATE_T);
            form.setDeptId(BaseInfo.GetUserRootDeptId(request));
            List<FormTh> formthes = formInsertVo.getElements();
            if(formthes==null)
                return rm.fail().info("请输入问题");
            formService.add(form);
            formThService.batchFormTh(formthes);
            rm.success().info("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            Form form = formInsertVo.getForm();
            formService.remove(form.getId());
            rm.fail().info("增加失败");
        }
        return rm;
//        ResultMap rm = new ResultMap();
//        try {
//            form.setState(mConst.STATE_T);
//            form.setDeptId(BaseInfo.GetUserRootDeptId(request));
//            formService.add(form);
//            rm.success().info("增加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            rm.fail().info("增加失败");
//        }
//        return rm;
    }

    @RequestMapping("show")
    @ResponseBody
    public ResultMap show(HttpServletRequest request,String id){
        ResultMap rm = new ResultMap();
        try{
            return rm.success().data(formService.showForm(id));
        }catch (Exception e){
            e.printStackTrace();
            rm.fail().info(e.getMessage());

        }
        return rm;

    }

    @RequestMapping("list")
    @ResponseBody
    public ResultMap list() {
        ResultMap rm = new ResultMap();
        try {
            Form form = new Form();
            form.setId(null);
            form.setState(mConst.STATE_T);
            List<Form> list = formService.list(form);
            rm.success().data(list);
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("page")
    @ResponseBody
    public ResultMap page(HttpServletRequest request, Form form) {
        ResultMap rm = new ResultMap();
        try {
            form.setId(null);
            form.setState(mConst.STATE_T);
            form.setDeptId(BaseInfo.GetUserRootDeptId(request));
            rm.success().page(formService.queryByPage(form));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }

    @RequestMapping("getById")
    @ResponseBody
    public ResultMap getById(String id) {
        ResultMap rm = new ResultMap();
        try {
            rm.success().data(formService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail"+e.getMessage());
        }
        return rm;
    }


    @RequestMapping("update")
    @ResponseBody
    public ResultMap update(Form form) {
        ResultMap rm = new ResultMap();
        try {
            formService.update(form);
            rm.success().info("修改成功");
        } catch (Exception e) {
            rm.fail().info("修改失败");
        }
        return rm;
    }

    @RequestMapping("remove")
    @ResponseBody
    public ResultMap remove(String id) {
        ResultMap rm = new ResultMap();
        try {
            formService.remove(id);
            rm.success().info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("删除失败");
        }
        return rm;
    }


}
